using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class SettingMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.cs_Menu, new Point(0, -107));

	private static UIMenuCheckboxItem CbxCanWearHelmetItem;

	private static UIMenuCheckboxItem CbxAutoAttackItem;

	private static UIMenuCheckboxItem CbxFollowItem;

	private static UIMenuCheckboxItem CbxInvincibleItem;

	private static List<dynamic> DrivingStyleText = new List<object>(new string[6]
	{
		TextSetting.cs_ListItem_DS_SOT,
		TextSetting.cs_ListItem_DS_ATE,
		TextSetting.cs_ListItem_DS_AT,
		TextSetting.cs_ListItem_DS_N,
		TextSetting.cs_ListItem_DS_IL,
		TextSetting.cs_ListItem_DS_R
	});

	private static UIMenuListItem DrivingStyleItem;

	private static List<dynamic> seatList = new List<object>(new string[6]
	{
		TextSetting.cs_ListItem_CS_LF,
		TextSetting.cs_ListItem_CS_RF,
		TextSetting.cs_ListItem_CS_LR,
		TextSetting.cs_ListItem_CS_RR,
		TextSetting.cs_ListItem_CS_ES1,
		TextSetting.cs_ListItem_CS_ES2
	});

	private static UIMenuListItem ChangeSeatItem;

	private static Origin origin;

	private static List<DrivingStyle> DrivingStyleList = new List<DrivingStyle>
	{
		DrivingStyle.SometimesOvertakeTraffic,
		DrivingStyle.AvoidTrafficExtremely,
		DrivingStyle.AvoidTraffic,
		DrivingStyle.Normal,
		DrivingStyle.IgnoreLights,
		DrivingStyle.Rushed
	};

	private static bool isMenuInitialized;

	public SettingMenu()
	{
		if (!isMenuInitialized)
		{
			Main.pool.Add(menu);
			menu.OnMenuClose += OnMenuClose;
			menu.OnCheckboxChange += OnCheckboxChange;
			menu.OnListChange += OnListChange;
			isMenuInitialized = true;
		}
	}

	public static void InitCharacterSettingMenu(Main.Member member)
	{
		menu.Clear();
		menu.SetBannerType(new UIResRectangle());
		if (member == Main.Member.P2)
		{
			origin = Main.P2;
		}
		else
		{
			origin = Main.P1;
		}
		CbxCanWearHelmetItem = new UIMenuCheckboxItem(TextSetting.cs_CbxItem_CanWearHelmet, origin.CanWearHelmet);
		CbxAutoAttackItem = new UIMenuCheckboxItem(TextSetting.cs_CbxItem_AutoAttack, origin.AutoAttack);
		CbxFollowItem = new UIMenuCheckboxItem(TextSetting.cs_CbxItem_Follow, origin.Follow);
		CbxInvincibleItem = new UIMenuCheckboxItem(TextSetting.cs_CbxItem_Invincible, origin.Invincible);
		DrivingStyleItem = new UIMenuListItem(TextSetting.cs_ListItem_DrivingStyle, DrivingStyleText, DrivingStyleList.IndexOf(origin.drivingStyle));
		ChangeSeatItem = new UIMenuListItem(TextSetting.cs_ListItem_ChangeSeat, seatList, origin.SeatIndex + 1);
		if (member == Main.Member.P2)
		{
			menu.AddItem(CbxCanWearHelmetItem);
			menu.AddItem(CbxAutoAttackItem);
			menu.AddItem(CbxFollowItem);
			menu.AddItem(CbxInvincibleItem);
			menu.AddItem(DrivingStyleItem);
			menu.AddItem(ChangeSeatItem);
		}
		else
		{
			menu.AddItem(CbxCanWearHelmetItem);
			menu.AddItem(DrivingStyleItem);
			menu.AddItem(ChangeSeatItem);
		}
	}

	private void OnCheckboxChange(UIMenu sender, UIMenuCheckboxItem item, bool Checked)
	{
		if (item.Text == CbxCanWearHelmetItem.Text)
		{
			if (Checked)
			{
				Origin obj = origin;
				bool canWearHelmet = (origin.Character.CanWearHelmet = true);
				obj.CanWearHelmet = canWearHelmet;
			}
			else
			{
				origin.Character.RemoveHelmet(instantly: true);
				Origin obj2 = origin;
				bool canWearHelmet = (origin.Character.CanWearHelmet = false);
				obj2.CanWearHelmet = canWearHelmet;
			}
		}
		if (item.Text == CbxAutoAttackItem.Text)
		{
			if (Checked)
			{
				origin.AutoAttack = true;
			}
			else
			{
				origin.AutoAttack = false;
			}
		}
		if (item.Text == CbxFollowItem.Text)
		{
			if (Checked)
			{
				origin.Character.Task.ClearAllImmediately();
				Main.P1.Character.CurrentPedGroup.Add(origin.Character, leader: false);
				origin.Character.PlayAmbientSpeech1("GENERIC_HI", immediately: true);
				origin.Follow = true;
			}
			else
			{
				origin.Character.PlayAmbientSpeech1("GENERIC_BYE", immediately: true);
				origin.Character.IsSpeechPlayingToEnd();
				origin.Character.LeaveGroup();
				origin.Character.Task.ClearAllImmediately();
				origin.Follow = false;
			}
		}
		if (item.Text == CbxInvincibleItem.Text)
		{
			if (Checked)
			{
				Origin obj3 = origin;
				bool canWearHelmet = (origin.Character.IsInvincible = true);
				obj3.Invincible = canWearHelmet;
			}
			else
			{
				Origin obj4 = origin;
				bool canWearHelmet = (origin.Character.IsInvincible = false);
				obj4.Invincible = canWearHelmet;
			}
		}
	}

	private void OnListChange(UIMenu sender, UIMenuListItem list, int index)
	{
		if (list.Text == DrivingStyleItem.Text)
		{
			origin.drivingStyle = DrivingStyleList[index];
			origin.Character.DrivingStyle = origin.drivingStyle;
		}
		if (origin.Character.IsInVehicle() && list.Text == ChangeSeatItem.Text && Main.IsSeatFree(origin.Character.CurrentVehicle, (VehicleSeat)MenuIndexToSeatId(index)))
		{
			origin.SeatIndex = MenuIndexToSeatId(index);
			origin.Character.Task.EnterVehicle(origin.Character.CurrentVehicle, (VehicleSeat)MenuIndexToSeatId(index), 3000, 2f, 16);
		}
	}

	public static int MenuIndexToSeatId(int index)
	{
		return index switch
		{
			0 => -1, 
			1 => 0, 
			2 => 1, 
			3 => 2, 
			4 => 3, 
			5 => 4, 
			_ => index, 
		};
	}

	private void OnMenuClose(UIMenu sender)
	{
		if (origin.Member == Main.Member.P1)
		{
			Main.P1 = origin;
		}
		else
		{
			Main.P2 = origin;
		}
		menu.Visible = false;
		menu.RefreshIndex();
	}
}
